﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using DBProvider;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCSurveyResult : System.Web.UI.UserControl
	{
		private SurveyProvider _SurveyProvider;

		private List<SurveyQuestion> lstSurveyQuestion;
		private Dictionary<int, List<SurveyQuestionItem>> dicSurveyAndSurveyQuestionItem;
		private List<SurveyAnswer> lstSurveyAnswer;

		public int SurveyID
		{
			get
			{
				return CommonHelper.QueryStringInt("surveyid");
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindSurvey();
			}
		}

		protected void rptSurveyQuestionItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if(e.Item.DataItem != null)
			{
				var ltrSurveyQuestionItem = e.Item.FindControl("ltrSurveyQuestionItem") as Literal;
				var ltrSurveyQuestionAnswer = e.Item.FindControl("ltrSurveyQuestionAnswer") as Literal;

				var dataItem = e.Item.DataItem as SurveyQuestionItem;

				ltrSurveyQuestionItem.Text = dataItem.SurveyQuestionItemLabel + ". " + dataItem.SurveyQuestionItemContent;

				if(this.lstSurveyAnswer != null && this.lstSurveyAnswer.Count > 0)
				{
					var surveyQuestion = this.lstSurveyQuestion.SingleOrDefault(sq => sq.SurveyQuestionID == dataItem.SurveyQuestionID);

					if(surveyQuestion.SurveyQuestionType == (int)SurveyQuestionType.SingleChoise || surveyQuestion.SurveyQuestionType == (int)SurveyQuestionType.MultiChoise)
					{
						var answersCount = lstSurveyAnswer.Count(sa => sa.SurveyAnswerSelectedQuestionItemID == dataItem.SurveyQuestionItemID);

						ltrSurveyQuestionAnswer.Text = answersCount.ToString() + "人选择";
					}
					//else if(surveyQuestion.SurveyQuestionType == (int)SurveyQuestionType.FillInBlank)
					//{ 
					//}
				}
			}
		}

		protected void rptSurveyQuestion_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if(e.Item.DataItem != null)
			{
				var rptSurveyQuestionItem = e.Item.FindControl("rptSurveyQuestionItem") as Repeater;
				var txtSurveyQuestionItem = e.Item.FindControl("txtSurveyQuestionItem") as TextBox;
				//var hfSurveyQuestionID = e.Item.FindControl("hfSurveyQuestionID") as HiddenField;

				//var theSurveyQuestionID = int.Parse(hfSurveyQuestionID.Value);

				var dataItem = e.Item.DataItem as SurveyQuestion;

				if(lstSurveyQuestion != null && lstSurveyQuestion.Count > 0)
				{
					var theSurveyQuestion = lstSurveyQuestion.SingleOrDefault(sq => sq.SurveyQuestionID == dataItem.SurveyQuestionID);

					if(theSurveyQuestion != null && theSurveyQuestion.SurveyQuestionType == (int)SurveyQuestionType.FillInBlank)
					{
						txtSurveyQuestionItem.Visible = true;

						if(this.lstSurveyAnswer != null && this.lstSurveyAnswer.Count > 0)
						{
							var answers = lstSurveyAnswer.Where(sa => sa.SurveyQuestionID == dataItem.SurveyQuestionID);

							if(answers != null)
							{
								List<string> lstAnswers = new List<string>();

								foreach(var item in answers)
								{
									if(!lstAnswers.Contains(item.SurveyAnswerContent))
									{
										lstAnswers.Add(item.SurveyAnswerContent);
									}
								}

								if(lstAnswers.Count > 0)
								{
									var strAnswers = string.Join(", ", lstAnswers);

									if(strAnswers.Length > 200)
									{
										txtSurveyQuestionItem.Text = strAnswers.Substring(0, 197) + "...";
									}
									else
									{
										txtSurveyQuestionItem.Text = strAnswers;
									}
								}
							}
						}
					}
					else
					{
						txtSurveyQuestionItem.Visible = false;

						if(dicSurveyAndSurveyQuestionItem != null && dicSurveyAndSurveyQuestionItem.Count > 0)
						{
							rptSurveyQuestionItem.ItemDataBound += new RepeaterItemEventHandler(rptSurveyQuestionItem_ItemDataBound);

							rptSurveyQuestionItem.DataSource = dicSurveyAndSurveyQuestionItem[dataItem.SurveyQuestionID];
							rptSurveyQuestionItem.DataBind();
						}
					}
				}
			}
		}

		private void BindSurvey()
		{
			if(SurveyID > 0)
			{
				if(_SurveyProvider == null)
				{
					_SurveyProvider = new SurveyProvider();
				}

				var theSurvey = _SurveyProvider.GetSurvey(SurveyID);

				if(theSurvey != null)
				{
					this.ltrSurveyTitle.Text = theSurvey.SurveyTitle;
					this.ltrSurveyDescription.Text = theSurvey.SurveyDescription;

					lstSurveyQuestion = _SurveyProvider.GetSurveyQuestionBySurvey(SurveyID);

					GetSurveyQuestionItem();

					this.lstSurveyAnswer = _SurveyProvider.GetSurveyAnswerBySurvey(SurveyID);

					rptSurveyQuestion.DataSource = lstSurveyQuestion;
					rptSurveyQuestion.DataBind();
				}
			}
		}

		private void GetSurveyQuestionItem()
		{
			if(SurveyID > 0)
			{
				if(_SurveyProvider == null)
				{
					_SurveyProvider = new SurveyProvider();
				}

				var sqi = _SurveyProvider.GetSurveyQuestionItemBySurvey(SurveyID);

				if(sqi != null && sqi.Count > 0)
				{
					this.dicSurveyAndSurveyQuestionItem = new Dictionary<int, List<SurveyQuestionItem>>();

					if(lstSurveyQuestion == null)
					{
						lstSurveyQuestion = _SurveyProvider.GetSurveyQuestionBySurvey(SurveyID);
					}

					if(lstSurveyQuestion != null && lstSurveyQuestion.Count > 0)
					{
						foreach(var sq in lstSurveyQuestion)
						{
							var lstSurveyQuestionItem = sqi.Where(s => s.SurveyQuestionID == sq.SurveyQuestionID);

							if(lstSurveyQuestionItem != null)
							{
								dicSurveyAndSurveyQuestionItem.Add(sq.SurveyQuestionID, lstSurveyQuestionItem.ToList());
							}
						}
					}
				}
			}
		}

		//private void GetSurveyAnswer()
		//{
		//  if(SurveyID > 0)
		//  {
		//    if(_SurveyProvider == null)
		//    {
		//      _SurveyProvider = new SurveyProvider();
		//    }

		//    this.lstSurveyAnswer = _SurveyProvider.GetSurveyAnswerBySurvey(SurveyID);
		//  }
		//}
	}
}